package com.scut.miaosha.controller;

import com.scut.miaosha.Result.Result;
import com.scut.miaosha.domain.User;
import com.scut.miaosha.rabbitmq.MQSender;
import com.scut.miaosha.redis.RedisService;
import com.scut.miaosha.redis.UserKey;
import com.scut.miaosha.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo")
public class HelloController {

    @Autowired
    public UserService userService;

    @Autowired
    public RedisService redisService;

    @Autowired
    MQSender sender;


    @RequestMapping("/thymeleaf")
    public String thymeleaf(Model model)
    {
        model.addAttribute("name","ELuF");
        return "hello";
    }


    @RequestMapping("/mq/header")
    @ResponseBody
    public Result<String> header() {
		sender.sendHeader("hello,imooc");
        return Result.success("Hello，world");
    }

	@RequestMapping("/mq/fanout")
    @ResponseBody
    public Result<String> fanout() {
		sender.sendFanout("hello,imooc");
        return Result.success("Hello，world");
    }

	@RequestMapping("/mq/topic")
    @ResponseBody
    public Result<String> topic() {
		sender.sendTopic("hello,imooc");
        return Result.success("Hello，world");
    }

	@RequestMapping("/mq")
    @ResponseBody
    public Result<String> mq() {
		sender.send("hello,imooc");
        return Result.success("Hello，world");
    }

    @RequestMapping("/db/get")
    @ResponseBody
    public Result<User> dbGet()
    {
        User user = userService.getById(1);
        return Result.success(user);
    }

    @RequestMapping("/redis/get")
    @ResponseBody
    public Result<User> redisGet(){
        User user = redisService.get(UserKey.getById,""+1,User.class);
        return Result.success(user);
    }

    @RequestMapping("/redis/set")
    @ResponseBody
    public Result<User> redisSet(){
        User user = new User();
        user.setId(1);
        user.setName("111111");
        System.out.println("1");
        boolean ret = redisService.set(UserKey.getById,""+1,user);
        System.out.println("2");
        User user1 = redisService.get(UserKey.getById,""+1 , User.class);
        System.out.println("3");
        return Result.success(user1);
    }



}
